-- EXEC SP_SAVE_SORT_LOT_BIN_DATA_ENTRY 1033, 1656, 2
-- CLOSE INFORMATIVE_BINNING_CURSOR
-- DEALLOCATE INFORMATIVE_BINNING_CURSOR

/*
CREATED BY	:	MURALI KRISHNA YENGALA
CREATED ON	:	7TH JULY 2005
PURPOSE		:	SAVES THE SORT BIN LOT DATA PRESENT IN INFORMATIVE_BINNING TABLE WHOSE BIN_1+BIN_2 VALUE EXCEEDS
        THE VALUE OF PARAMETER KEY 'MRB LIMIT', WHEN THE LOT IS CLOSED
*/

ALTER    PROCEDURE SP_SAVE_SORT_LOT_BIN_DATA_ENTRY

@PROCESS_STEP_ID 	INT,
@LOT_ID 		INT,
-- @BIN_COUNT is the 'good bins count'
@BIN_COUNT      INT

AS
BEGIN
DECLARE @TMP_IDX        INT
DECLARE @WAFER_ID	    INT
DECLARE @BIN_1		    INT
DECLARE @BIN_2		    INT
DECLARE @BIN_3		    INT
DECLARE @BIN_4		    INT
DECLARE @BIN_5		    INT
DECLARE @BIN_6		    INT
DECLARE @BIN_7		    INT
DECLARE @BIN_8		    INT
DECLARE @BIN_9		    INT
DECLARE @BIN_10		    INT
DECLARE @BIN_11		    INT
DECLARE @BIN_12		    INT
DECLARE @BIN_13		    INT
DECLARE @BIN_14		    INT
DECLARE @BIN_15		    INT
DECLARE @RMA_LIMIT	    INT
DECLARE @MRB_LIMIT	    INT
DECLARE @DOWN_BIN_LIMIT INT
DECLARE @BIN_SUM_STR    VARCHAR(500)
DECLARE @SQL_QUERY      NVARCHAR(1000)
DECLARE @PARAM_DEFINITION      NVARCHAR(250)
DECLARE @BIN_VALUES	VARCHAR(100)
DECLARE @downBin_number	VARCHAR(50)
DECLARE @BASE_PRODUCT_ID INT
DECLARE @GOOD_QTY	INT
DECLARE @COUNT		INT
DECLARE @ENGINEERING_COUNT INT

SELECT @RMA_LIMIT = PARAMETER_VALUE FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND PARAMETER_KEY LIKE 'RMA Limit'
SELECT @MRB_LIMIT = PARAMETER_VALUE FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND PARAMETER_KEY LIKE 'MRB Limit'
SELECT @DOWN_BIN_LIMIT = CASE LTRIM(RTRIM(PARAMETER_VALUE)) WHEN '' THEN '0' WHEN NULL THEN '0' ELSE LTRIM(RTRIM(PARAMETER_VALUE)) END FROM PROCESS_PARAMETER WHERE PROCESSSTEP_ID = @PROCESS_STEP_ID AND PARAMETER_KEY LIKE 'Down%'

IF  ( @DOWN_BIN_LIMIT IS NULL )
BEGIN
	SET @DOWN_BIN_LIMIT = 0
END

/* 
The following block will calculate the sum of all the good bins 
The value in @BIN_SUM_STR will be in the following format.
ISNULL(BIN_1,0)+ISNULL(BIN_2,0)+...+ISNULL(BIN_N,0) where 'N' is the 'good bin count'
*/
SET @TMP_IDX = 0
SET @BIN_SUM_STR = ''
WHILE @TMP_IDX != @BIN_COUNT
BEGIN
    SET @BIN_SUM_STR = LTRIM(RTRIM(@BIN_SUM_STR)) + 'ISNULL(BIN_' + CONVERT( VARCHAR, @TMP_IDX+1 ) + ',0)'
    SET @TMP_IDX = @TMP_IDX + 1
    IF @TMP_IDX != @BIN_COUNT
        BEGIN
            SET @BIN_SUM_STR = LTRIM(RTRIM(@BIN_SUM_STR)) + '+'
        END
END


SELECT @BASE_PRODUCT_ID = P.P_PARENT_PRODUCT_ID FROM LOT_TABLE LT, PRODUCT P WHERE LT.PROCESSSTEP_ID = P.PROCESSSTEP_ID AND LOT_ID = @LOT_ID

/*
INFORMATIVE_BINNING_CURSOR will have those wafers, whose good dice quantity is greater than RMA LIMIT
The wafers that are selected will have the RMA_LIMT_FLAG and MRB_LIMIT_FLAG as following.
1, 1 -> (GOOD DICE QUANTITY > MRB LIMIT)  OR (RMA LIMIT < GOOD DICE QUANTITY <= MRB LIMIT, IF THE WAFER IS RELEASED FROM MRB RELEASE SCREEN)

Wafers with the following RMS_LIMIT_FLAG and MRB_LIMIT_FLAG are not selected for the resons explained alongside
1, 0 -> (RMA LIMIT < GOOD DICE QUANTITY <= MRB LIMIT) THESE WAFERS ARE NOT ELIGIBLE FOR ASSEMBLY RELEASE
0, 1 -> GOOD DICE QUANTITY IS <= RMA LIMIT, WHICH MEANS THAT THE WAFER IS SCRAPPED
0, 0 -> THIS COMBINATION IS NOT POSSIBLE, SINCE GOOD DICE QUANTITY CANNOT BE <= RMA LIMIT AND  RMA LIMIT < GOOD DICE QUANTITY <= MRB LIMIT
*/
SET @SQL_QUERY = N'DECLARE INFORMATIVE_BINNING_CURSOR CURSOR FOR SELECT WAFER_ID, BIN_1, BIN_2, BIN_3, BIN_4, BIN_5,
	BIN_6, BIN_7, BIN_8, BIN_9, BIN_10, BIN_11, BIN_12, BIN_13, BIN_14, BIN_15,' + @BIN_SUM_STR + ' AS GOOD_QTY FROM INFORMATIVE_BINNING WHERE LOT_ID = @LOT_ID  AND NOT ( MRB_LIMIT = 0 OR RMA_LIMIT = 0 )'


PRINT @SQL_QUERY
SET @PARAM_DEFINITION = N'@LOT_ID INT, @MRB_LIMIT INT, @DOWN_BIN_LIMIT INT'
EXECUTE SP_EXECUTESQL @SQL_QUERY, @PARAM_DEFINITION, @LOT_ID = @LOT_ID, @MRB_LIMIT = @MRB_LIMIT, @DOWN_BIN_LIMIT = @DOWN_BIN_LIMIT

OPEN INFORMATIVE_BINNING_CURSOR
FETCH NEXT FROM INFORMATIVE_BINNING_CURSOR INTO @WAFER_ID, @BIN_1, @BIN_2, @BIN_3, @BIN_4, @BIN_5, @BIN_6,
                        @BIN_7, @BIN_8, @BIN_9, @BIN_10, @BIN_11, @BIN_12, @BIN_13,
                        @BIN_14, @BIN_15, @GOOD_QTY 
WHILE @@FETCH_STATUS = 0
BEGIN
	--TO CHECK WHETHER THE ASSEMBLY RELEASE IS ISSUED OR NOT FOR THE LOT AND WAFER ID
	SET @COUNT = 0
	SET @ENGINEERING_COUNT = 0
	SELECT @COUNT = COUNT(*) FROM Assm_Release_Lot_Table ARLT, Assm_Release_Table ART  WHERE Lot_id = @LOT_ID AND Wafer_Id = @WAFER_ID AND ART.Assembly_Release_Id = ARLT.Assembly_Release_Id AND ART.Status <> 'CANCELLED'
	SELECT @ENGINEERING_COUNT = COUNT(*) FROM ENGINEERING_LOTS  WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
	IF ( @COUNT = 0 AND @ENGINEERING_COUNT = 0 ) --NO ASSEMBLY RELEASE IS ISSUED AND IS NOT MOVED FOR ENGINEERING.
	BEGIN

		--Good Qty Greater than Down Bin Limit (OR) Released through the MRB Release Screen.
		IF ( ( ( @DOWN_BIN_LIMIT <> 0 ) AND ( @GOOD_QTY > @DOWN_BIN_LIMIT ) ) OR  
		     ( ( @DOWN_BIN_LIMIT <> 0 ) AND ( @MRB_LIMIT < @GOOD_QTY ) AND ( @GOOD_QTY <= @DOWN_BIN_LIMIT ) ) OR
		     ( ( @DOWN_BIN_LIMIT = 0 ) AND ( @GOOD_QTY > @MRB_LIMIT ) ) OR
		     ( ( @RMA_LIMIT < @GOOD_QTY ) AND ( @GOOD_QTY <= @MRB_LIMIT ) ) )	
		BEGIN

			--TO CHECK WHETHER THIS LOT AND WAFER ID EXISTS IN SORT_BIN_RELEASE_TABLE
			SELECT @COUNT = COUNT(*) FROM Sort_Bin_Release_Table WHERE Lot_Id = @LOT_ID AND Wafer_Id = @WAFER_ID
			IF ( @COUNT > 0 ) --RECORD EXISTS IN SORT_BIN_RELEASE_TABLE, NEED TO UPDATE
			BEGIN
				UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = ISNULL(@BIN_1,0),  BIN_2 = ISNULL(@BIN_2,0),  
					BIN_3 = ISNULL(@BIN_3,0),  BIN_4 = ISNULL(@BIN_4,0),  BIN_5 = ISNULL(@BIN_5,0) ,  
					BIN_6 = ISNULL(@BIN_6,0),  BIN_7 = ISNULL(@BIN_7,0),  BIN_8 = ISNULL(@BIN_8,0),  
					BIN_9 = ISNULL(@BIN_9,0),  BIN_10 = ISNULL(@BIN_10,0),  BIN_11 = ISNULL(@BIN_11,0),  
					BIN_12 = ISNULL(@BIN_12,0),  BIN_13 = ISNULL(@BIN_13,0),  BIN_14 = ISNULL(@BIN_14,0),  
					BIN_15 = ISNULL(@BIN_15,0),  BIN_1_BALANCE_QTY = ISNULL(@BIN_1,0),  
					BIN_2_BALANCE_QTY = ISNULL(@BIN_2,0),  BIN_3_BALANCE_QTY = ISNULL(@BIN_3,0),  
					BIN_4_BALANCE_QTY = ISNULL(@BIN_4,0),  BIN_5_BALANCE_QTY = ISNULL(@BIN_5,0),  
					BIN_6_BALANCE_QTY = ISNULL(@BIN_6,0),  BIN_7_BALANCE_QTY = ISNULL(@BIN_7,0),  
					BIN_8_BALANCE_QTY = ISNULL(@BIN_8,0),  BIN_9_BALANCE_QTY = ISNULL(@BIN_9,0),  
					BIN_10_BALANCE_QTY = ISNULL(@BIN_10,0),  BIN_11_BALANCE_QTY = ISNULL(@BIN_11,0),  
					BIN_12_BALANCE_QTY = ISNULL(@BIN_12,0),  BIN_13_BALANCE_QTY = ISNULL(@BIN_13,0),  
					BIN_14_BALANCE_QTY = ISNULL(@BIN_14,0),  BIN_15_BALANCE_QTY = ISNULL(@BIN_15,0) ,
					IS_DOWN_GRADE_BIN = 1 
				WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
			END
			ELSE --DOES NOT EXISTS IN SORT_BIN_RELEASE_TABLE
			BEGIN
			    	INSERT INTO SORT_BIN_RELEASE_TABLE( LOT_ID, WAFER_ID, BIN_1, BIN_2, BIN_3, BIN_4, BIN_5, BIN_6, BIN_7,
			        BIN_8, BIN_9, BIN_10, BIN_11, BIN_12, BIN_13, BIN_14, BIN_15, BIN_1_BALANCE_QTY,
			        BIN_2_BALANCE_QTY, BIN_3_BALANCE_QTY, BIN_4_BALANCE_QTY, BIN_5_BALANCE_QTY, BIN_6_BALANCE_QTY,
			        BIN_7_BALANCE_QTY, BIN_8_BALANCE_QTY, BIN_9_BALANCE_QTY, BIN_10_BALANCE_QTY, BIN_11_BALANCE_QTY,
			        BIN_12_BALANCE_QTY, BIN_13_BALANCE_QTY, BIN_14_BALANCE_QTY, BIN_15_BALANCE_QTY, IS_DOWN_GRADE_BIN )
			        VALUES (@LOT_ID, @WAFER_ID, ISNULL(@BIN_1,0), ISNULL(@BIN_2,0),
			        ISNULL(@BIN_3,0), ISNULL(@BIN_4,0), ISNULL(@BIN_5,0), ISNULL(@BIN_6,0), ISNULL(@BIN_7,0),
			        ISNULL(@BIN_8,0), ISNULL(@BIN_9,0), ISNULL(@BIN_10,0), ISNULL(@BIN_11,0), ISNULL(@BIN_12,0),
			        ISNULL(@BIN_13,0), ISNULL(@BIN_14,0), ISNULL(@BIN_15,0), ISNULL(@BIN_1,0), ISNULL(@BIN_2,0),
			        ISNULL(@BIN_3,0), ISNULL(@BIN_4,0), ISNULL(@BIN_5,0), ISNULL(@BIN_6,0),
			        ISNULL(@BIN_7,0), ISNULL(@BIN_8,0), ISNULL(@BIN_9,0), ISNULL(@BIN_10,0), ISNULL(@BIN_11,0),
			        ISNULL(@BIN_12,0), ISNULL(@BIN_13,0), ISNULL(@BIN_14,0), ISNULL(@BIN_15,0), 1 )
			END
		END	
		--Good Qty Greater than Down Bin Limit (OR) Released through the MRB Release Screen.

		/*
		DOWNGRADING is performed for the wafer under the following conditions
		1. MRB LIMIT < GOOD DICE QUANTITY <= DOWN BIN LIMIT ( RMA_LIMIT_FLAG = 1, MRB_LIMIT_FLAG = 1)
		2. RMA LIMIT < GOOD DICE QUANTITY <= MRB LIMIT IF THE WAFER IS RELEASED IN MRB RELEASE SCREEN ( RMA_LIMIT_FLAG = 1, MRB_LIMIT_FLAG = 1)
		3. DOWN_BIN_LIMIT IS NOT SET
		*/

		--IF ( NOT ( @DOWN_BIN_LIMIT != 0 AND @DOWN_BIN_LIMIT < @GOOD_QTY ))
		IF ( ( ( @DOWN_BIN_LIMIT <> 0 ) AND ( @MRB_LIMIT < @GOOD_QTY ) AND ( @GOOD_QTY <= @DOWN_BIN_LIMIT ) ) OR
		     ( ( @RMA_LIMIT < @GOOD_QTY ) AND ( @GOOD_QTY <= @MRB_LIMIT ) ) )	
		BEGIN

			/*
			The following code will add the current bin's quantity to the bin it is downgraded to, after setting the current bin's quantity to '0'
			*/
			IF @BIN_1 > 0
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin1'
				IF @downBin_number IS NOT NULL AND LEN(LTRIM(RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					IF @downBin_number = 'BIN2'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_2 = ISNULL( BIN_2, 0 ) + @BIN_1, BIN_2_BALANCE_QTY = ISNULL( BIN_2_BALANCE_QTY, BIN_2 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN3'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_3 = ISNULL( BIN_3, 0 ) + @BIN_1, BIN_3_BALANCE_QTY = ISNULL( BIN_3_BALANCE_QTY, BIN_3 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN4'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_1, BIN_4_BALANCE_QTY = ISNULL( BIN_4_BALANCE_QTY, BIN_4 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN5'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_1, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN6'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_1, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN7'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_1, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_1, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_1, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_1, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_1, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_1, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_1, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_1, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_1 = 0 ,BIN_1_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_1, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_1, IS_DOWN_GRADE_BIN = 0 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
		
				END
			END
			IF @BIN_2 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin2'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					IF @downBin_number = 'BIN3'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_3 = ISNULL( BIN_3, 0 ) + @BIN_2, BIN_3_BALANCE_QTY = ISNULL( BIN_3_BALANCE_QTY, BIN_3 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN4'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_2, BIN_4_BALANCE_QTY = ISNULL( BIN_4_BALANCE_QTY, BIN_4 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN5'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_2, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN6'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_2, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN7'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_2, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_2, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_2, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_2, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_2, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_2, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_2, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_2, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_2 = 0 ,BIN_2_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_2, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_2 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_3 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin3'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN4'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_4 = ISNULL( BIN_4, 0 ) + @BIN_3, BIN_4_BALANCE_QTY = ISNULL( BIN_4_BALANCE_QTY, BIN_4 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN5'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_3, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN6'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_3, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN7'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_3, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_3, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_3, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_3, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_3, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_3, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_3, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_3, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_3 = 0 ,BIN_3_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_3, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_3 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_4 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin4'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN5'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_5 = ISNULL( BIN_5, 0 ) + @BIN_4, BIN_5_BALANCE_QTY = ISNULL( BIN_5_BALANCE_QTY, BIN_5 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN6'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_4, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN7'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_4, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_4, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_4, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_4, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_4, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_4, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_4, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_4, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_4 = 0 ,BIN_4_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_4, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_4 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_5 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin5'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					IF @downBin_number = 'BIN6'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_6 = ISNULL( BIN_6, 0 ) + @BIN_5, BIN_6_BALANCE_QTY = ISNULL( BIN_6_BALANCE_QTY, BIN_6 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN7'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_5, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_5, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_5, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_5, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_5, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_5, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_5, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_5, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_5 = 0 ,BIN_5_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_5, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_5 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_6 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin6'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					IF @downBin_number = 'BIN7'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_7 = ISNULL( BIN_7, 0 ) + @BIN_6, BIN_7_BALANCE_QTY = ISNULL( BIN_7_BALANCE_QTY, BIN_7 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_6, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_6, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_6, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_6, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_6, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_6, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_6, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_6 = 0 ,BIN_6_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_6, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_6 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_7 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin7'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN8'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET  BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_8 = ISNULL( BIN_8, 0 ) + @BIN_7, BIN_8_BALANCE_QTY = ISNULL( BIN_8_BALANCE_QTY, BIN_8 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_7, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_7, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_7, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_7, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_7, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_7, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_7 = 0 ,BIN_7_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_7, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_7 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_8 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin8'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN9'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_9 = ISNULL( BIN_9, 0 ) + @BIN_8, BIN_9_BALANCE_QTY = ISNULL( BIN_9_BALANCE_QTY, BIN_9 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_8, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_8, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_8, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_8, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_8, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_8 = 0 ,BIN_8_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_8, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_8 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_9 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin9'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN10'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_10 = ISNULL( BIN_10, 0 ) + @BIN_9, BIN_10_BALANCE_QTY = ISNULL( BIN_10_BALANCE_QTY, BIN_10 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_9, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_9, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_9, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_9, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_9 = 0 ,BIN_9_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_9, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_9 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_10 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin10'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN11'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_11 = ISNULL( BIN_11, 0 ) + @BIN_10, BIN_11_BALANCE_QTY = ISNULL( BIN_11_BALANCE_QTY, BIN_11 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_10, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_10, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_10, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_10 = 0 ,BIN_10_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_10, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_10 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_11 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin11'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					IF @downBin_number = 'BIN12'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_12 = ISNULL( BIN_12, 0 ) + @BIN_11, BIN_12_BALANCE_QTY = ISNULL( BIN_12_BALANCE_QTY, BIN_12 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_11, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_11, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_11 = 0 ,BIN_11_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_11, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_11 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_12 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin12'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN13'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = 0 ,BIN_12_BALANCE_QTY = 0 ,BIN_13 = ISNULL( BIN_13, 0 ) + @BIN_12, BIN_13_BALANCE_QTY = ISNULL( BIN_13_BALANCE_QTY, BIN_13 ) + @BIN_12 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = 0 ,BIN_12_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_12, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_12 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_12 = 0 ,BIN_12_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_12, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_12 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_13 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin13'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					
					IF @downBin_number = 'BIN14'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_13 = 0 ,BIN_13_BALANCE_QTY = 0 ,BIN_14 = ISNULL( BIN_14, 0 ) + @BIN_13, BIN_14_BALANCE_QTY = ISNULL( BIN_14_BALANCE_QTY, BIN_14 ) + @BIN_13 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_13 = 0 ,BIN_13_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_13, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_13 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
				END
			END
			IF @BIN_14 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin14'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					IF @downBin_number = 'BIN15'
					BEGIN
						UPDATE SORT_BIN_RELEASE_TABLE SET BIN_14 = 0 ,BIN_14_BALANCE_QTY = 0 ,BIN_15 = ISNULL( BIN_15, 0 ) + @BIN_14, BIN_15_BALANCE_QTY = ISNULL( BIN_15_BALANCE_QTY, BIN_15 ) + @BIN_14 WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
					END
		
				END
			END
			IF @BIN_15 > 0 
			BEGIN
				SELECT @downBin_number = Downsize_Bin_Number FROM MRP_Binning_Mapping WHERE Base_Product_ID = @BASE_PRODUCT_ID AND Bin_Number = 'Bin15'
				IF @downBin_number IS NOT NULL AND LEN( LTRIM( RTRIM( @downBin_number ) ) ) > 1
				BEGIN
					UPDATE SORT_BIN_RELEASE_TABLE SET LOT_ID = 'LOTID' WHERE LOT_ID = @LOT_ID AND WAFER_ID = @WAFER_ID
				END
			END
		END
	 END
	/*ELSE -- ASSEBLY RELEASE IS ISSUED FOR THIS LOT AND WAFER ID SO WE R NOT TOUCHING IT
	 BEGIN
	 END*/

FETCH NEXT FROM INFORMATIVE_BINNING_CURSOR INTO @WAFER_ID, @BIN_1, @BIN_2, @BIN_3, @BIN_4, @BIN_5, @BIN_6,
                        @BIN_7, @BIN_8, @BIN_9, @BIN_10, @BIN_11, @BIN_12, @BIN_13,
                        @BIN_14, @BIN_15, @GOOD_QTY 
END
CLOSE INFORMATIVE_BINNING_CURSOR
DEALLOCATE INFORMATIVE_BINNING_CURSOR
END
